package com.example.blog.base;

import com.example.blog.exception.CommonErrorCode;
import lombok.Data;
import org.springframework.web.bind.annotation.ControllerAdvice;

import java.io.Serializable;

/**
 * @Classname Result
 * @Description TODO 统一返回结果集
 * @Version 1.0.0
 * @Date 2023/1/26 12:23
 * @Created by wlh12
 */
@Data
@ControllerAdvice
public class Result<T> implements Serializable {

    private Integer code = 200;
    private T data;
    private String message;
    private Boolean isMessage = false;


    public static Result<Void> success(){
        return success("操作成功",false);
    }

    public static Result<Void> success(String message){
        return success(message,false);
    }

    public static<T> Result<T> success(String message, Boolean isMessage){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setData(null);
        result.setMessage(message);
        result.setIsMessage(isMessage);
        return result;
    }
    public static<T> Result<T> successData(T data,String message, Boolean isMessage){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setData(data);
        result.setMessage(message);
        result.setIsMessage(isMessage);
        return result;
    }

    public static<T> Result<T> successData(T data,String message){
        return successData(data,message,false);
    }

    public static<T> Result<T> successData(T data){
        return successData(data,"操作成功",false);
    }


    public static Result<Void> fail(){
        Result<Void> result = new Result<>();
        result.setCode(CommonErrorCode.BUSINESS_ERROR.getCode());
        result.setData(null);
        result.setMessage("操作失败");
        result.setIsMessage(true);
        return result;
    }

    public static Result<Void> fail(String message ,Integer code){
        Result<Void> result = new Result<>();
        result.setCode(code);
        result.setData(null);
        result.setMessage(message);
        result.setIsMessage(true);
        return result;
    }

    public static Result<Void> fail(String message){
        Result<Void> result = new Result<>();
        result.setCode(CommonErrorCode.BUSINESS_ERROR.getCode());
        result.setData(null);
        result.setMessage(message);
        result.setIsMessage(true);
        return result;
    }
}
